package com.customview.custRadarview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2021/7/7 14:17
 */
public class CustRadarview extends View {
    public CustRadarview(Context context) {
        this(context,null);
    }

    public CustRadarview(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    Paint BgPaint;//背景画笔
    Paint TvPaint;//文字画笔
    Paint CirclePaint;//
    Paint RadarPaint;//中间阴影画笔
    public CustRadarview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        init();



    }
    public void setValues( ArrayList<ValueObject> values){
        if (values.size()==3){
            valueObjects=values;
            postInvalidate();
        }else {

        }

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec),getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec));

    }
    //初始化画笔
    int radius;
    float WidthMargin;
    //雷电线
    int RadarSize=4;
    //画布边距
    int DefaultMargin=dp2px(20);
    ArrayList<ValueObject> valueObjects;
    private void init()
    {
        valueObjects=new ArrayList<>();
        BgPaint=new Paint();
        BgPaint.setStyle(Paint.Style.STROKE);
        BgPaint.setColor(Color.parseColor("#559DFE"));
        BgPaint.setAlpha(100);
        BgPaint.setStrokeWidth(5);
        BgPaint.setAntiAlias(true);

        TvPaint=new Paint();
        TvPaint.setTextSize(dp2px(12));
        TvPaint.setColor(Color.WHITE);
        TvPaint.setTextAlign(Paint.Align.LEFT);


        RadarPaint=new Paint();
        RadarPaint.setStyle(Paint.Style.FILL);
        RadarPaint.setAntiAlias(true);

        CirclePaint=new Paint();
        CirclePaint.setAntiAlias(true);


         radius=getMeasuredWidth()/2-DefaultMargin;
         WidthMargin =  radius/RadarSize;

        ValueObject valueObject1 = new ValueObject(80, "力度");
        ValueObject valueObject2 = new ValueObject(60, "区域");
        ValueObject valueObject3 = new ValueObject(20, "时长");
        valueObjects.add(valueObject1);
        valueObjects.add(valueObject2);
        valueObjects.add(valueObject3);


    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBg(canvas);
//        canvas.translate(getWidth()/2,getHeight()/2);
//        canvas.rotate(-40);
//        for (int i = 0; i <pathList.size(); i++) {
//            Log.d("test", "onDraw: "+i);
//            canvas.drawPath(pathList.get(i),BgPaint);
//        }
        draRadar(canvas);
        drawCircle(canvas);

    }

    float CircleSize=dp2px(4);
    float CircleStroke=dp2px(2);
    private void drawCircle(Canvas canvas) {
        float pointX=radius;
        float pointY=0;

        float pointX1 = (float) (radius* Math.cos(Math.toRadians(360/3)));
        float pointY1 = (float) (radius* Math.sin(Math.toRadians(360/3)));

        float pointX2 = (float) (radius* Math.cos(Math.toRadians(360/3*2)));
        float pointY2 = (float) (radius* Math.sin(Math.toRadians(360/3*2)));



        float mpointX = getX(valueObjects.get(0).getValue(), 0);
        float mpointY = getY(valueObjects.get(0).getValue(), 0);
        float mpointX1 = getX(valueObjects.get(1).getValue(), 360 / 3);
        float mpointY1 = getY(valueObjects.get(1).getValue(), 360 / 3);
        float mpointX2 = getX(valueObjects.get(2).getValue(), 360/3*2);
        float mpointY2 = getY(valueObjects.get(2).getValue(), 360/3*2);
        CirclePaint.setStyle(Paint.Style.FILL);
        CirclePaint.setColor(Color.parseColor("#64ACFF"));
        canvas.drawCircle(pointX,pointY,CircleSize,CirclePaint);
        canvas.drawCircle(pointX1,pointY1,CircleSize,CirclePaint);
        canvas.drawCircle(pointX2,pointY2,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX,mpointY,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX1,mpointY1,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX2,mpointY2,CircleSize,CirclePaint);
        CirclePaint.setStyle(Paint.Style.STROKE);
        CirclePaint.setStrokeWidth(CircleStroke);
        CirclePaint.setColor(Color.parseColor("#404040"));
        canvas.drawCircle(pointX,pointY,CircleSize,CirclePaint);
        canvas.drawCircle(pointX1,pointY1,CircleSize,CirclePaint);
        canvas.drawCircle(pointX2,pointY2,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX,mpointY,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX1,mpointY1,CircleSize,CirclePaint);
        canvas.drawCircle(mpointX2,mpointY2,CircleSize,CirclePaint);
    }

    //
    private void draRadar(Canvas canvas) {



        if (valueObjects.isEmpty())
            return;

        Path mpath=new Path();
        float pointX = getX(valueObjects.get(0).getValue(), 0);
        float pointY = getY(valueObjects.get(0).getValue(), 0);
        mpath.moveTo(pointX,pointY);
        float pointX1 = getX(valueObjects.get(1).getValue(), 360 / 3);
        float pointY1 = getY(valueObjects.get(1).getValue(), 360 / 3);
        mpath.quadTo(pointX1+dp2px(20),pointY1+dp2px(20),pointX1,pointY1);
        float pointX2 = getX(valueObjects.get(2).getValue(), 360/3*2);
        float pointY2 = getY(valueObjects.get(2).getValue(), 360/3*2);
        mpath.quadTo(pointX2-dp2px(20),pointY2+dp2px(20),pointX2,pointY2);
        mpath.quadTo(pointX-dp2px(20),pointY-dp2px(20),pointX,pointY);
        
        mpath.close();
        //遮盖层
        RadarPaint.setStyle(Paint.Style.FILL);
        RadarPaint.setColor(Color.parseColor("#5587FE"));
        RadarPaint.setAlpha(100);

        canvas.drawPath(mpath,RadarPaint);
        //描边
        RadarPaint.setStyle(Paint.Style.STROKE);
        RadarPaint.setStrokeWidth(10);
        RadarPaint.setColor(Color.parseColor("#559DFE"));
        canvas.drawPath(mpath,RadarPaint);

        canvas.rotate(40,pointX,pointY);
        canvas.drawText(valueObjects.get(0).getName(),pointX,pointY,TvPaint);
        canvas.rotate(-40,pointX,pointY);
        canvas.rotate(40,pointX1,pointY1);
        canvas.drawText(valueObjects.get(1).getName(),pointX1,pointY1,TvPaint);
        canvas.rotate(-40,pointX1,pointY1);
        canvas.rotate(40,pointX2,pointY2);
        canvas.drawText(valueObjects.get(2).getName(),pointX2,pointY2,TvPaint);
        canvas.rotate(-40,pointX2,pointY2);



    }


    private void drawBg(Canvas canvas) {

        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.rotate(-40);

        for (int i = 0; i <RadarSize ; i++) {
            Path mpath=new Path();
//            mpath.moveTo(radius-WidthMargin*i,0);
//            float pointX = (float) ((radius-WidthMargin*i)* Math.cos(Math.toRadians(360/3)));
//            float pointY = (float) ((radius-WidthMargin*i)* Math.sin(Math.toRadians(360/3)));
//            mpath.lineTo(pointX,pointY);
//            float pointX2 = (float) ((radius-WidthMargin*i)* Math.cos(Math.toRadians(360/3*2)));
//            float pointY2 = (float) ((radius-WidthMargin*i)* Math.sin(Math.toRadians(360/3*2)));
//            mpath.lineTo(pointX2,pointY2);
//            mpath.close();


            float smallRadius=dp2px(5);
            float pointX0 = (float) ((radius-WidthMargin*i-smallRadius)* Math.cos(Math.toRadians(0)));
            float pointY0 = (float) ((radius-WidthMargin*i-smallRadius)* Math.sin(Math.toRadians(0)));
            RectF rectF1= new RectF(pointX0-smallRadius,pointY0-smallRadius,pointX0+smallRadius,pointY0+smallRadius);
            mpath.arcTo(rectF1,-45,90);
            float pointX = (float) ((radius-WidthMargin*i-smallRadius)* Math.cos(Math.toRadians(360/3)));
            float pointY = (float) ((radius-WidthMargin*i-smallRadius)* Math.sin(Math.toRadians(360/3)));
            RectF rectF2=new RectF(pointX-smallRadius,pointY-smallRadius,pointX+smallRadius,pointY+smallRadius);
            mpath.arcTo(rectF2,120-45,90);
            float pointX2 = (float) ((radius-WidthMargin*i-smallRadius)* Math.cos(Math.toRadians(360/3*2)));
            float pointY2 = (float) ((radius-WidthMargin*i-smallRadius)* Math.sin(Math.toRadians(360/3*2)));
            RectF rectF3=new RectF(pointX2-smallRadius,pointY2-smallRadius,pointX2+smallRadius,pointY2+smallRadius);
            mpath.arcTo(rectF3,240-45,90);


            mpath.close();






            canvas.drawPath(mpath,BgPaint);


        }





    }

    int MaxValue=100;
    public float getX(float value,double angle){
        float pointX = (float) ((radius*(value/MaxValue))* Math.cos(Math.toRadians(angle)));
    return pointX;
    }
   public float getY(float value,double angle){
        float pointY = (float) ((radius*(value/MaxValue))* Math.sin(Math.toRadians(angle)));
    return pointY;
    }
    private  int dp2px( float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getContext().getResources().getDisplayMetrics());
    }

    public class ValueObject{
        float Value;
        String Name;

        public ValueObject(float value, String name) {
            Value = value;
            Name = name;
        }

        public float getValue() {
            return Value;
        }

        public void setValue(float value) {
            Value = value;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }
    }

}
